#define  _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

void Init(Stack* ps)
{
	assert(ps);
	ps->a = (DataType*)malloc(sizeof(DataType) * 4);
	if (ps->a == NULL)
	{
		printf("malloc fail");
		return;
	}
	ps->capicity = 4;
	ps->top = -1;
}

void Push(Stack* ps, DataType x)
{
	assert(ps);
	if (ps->top == ps->capicity - 1)
	{
		DataType* tmp = (DataType*)realloc(ps->a, sizeof(DataType) * ps->capicity * 2);
		if (tmp == NULL)
		{
			printf("realloc fail");
			return;
		}
		ps->a = tmp;
		ps->capicity *= 2;
	}
	ps->top++;
	ps->a[ps->top] = x;
}

void Pop(Stack* ps)
{
	assert(ps);
	assert(!IsEmpty(ps));
	ps->top--;
}

DataType GetTop(Stack* ps)
{
	assert(ps);
	assert(!IsEmpty(ps));

	return ps->a[ps->top];
}

bool IsEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == -1;
}

void Destroy(Stack* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capicity = 0;
	ps->top = -1;
}